package com.entrogy.dao;

import com.entrogy.bean.WeixinMenuEntity;
import com.entrogy.bean.WeixinTypeEntity;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

@Mapper
public interface WeixinMenuMapper {

    @Insert("insert into weixin_menu(menu_name,path_url,type,school_code) values (#{menuName},#{pathUrl},#{type},#{schoolCode})")
    int insertWeixinMenu(WeixinMenuEntity weixinMenuEntity);

    @Update("update weixin_menu set menu_name = #{menuName},path_url = #{pathUrl},type = #{type} where id = #{id}")
    int updateWeixinMenuById(WeixinMenuEntity weixinMenuEntity);

    @Delete("delete from weixin_menu where id = #{id}")
    int deleteWeixinMenuById(@Param("id") int id);

    @Results({
            @Result(property="weixinTypeEntities",column = "type",many=@Many(select="com.entrogy.dao.WeixinMenuMapper.getWeixinTypByType",fetchType= FetchType.LAZY))
    })
    @Select("select w.*,s.school_name from weixin_menu w left join school_information s on w.school_code = s.school_code")
    List<WeixinMenuEntity> getWeixinMenus();

    @Select("select w.* from weixin_menu w where school_code = #{schoolCode} and FIND_IN_SET(#{type},w.type)")
    List<WeixinMenuEntity> getWeixinMenuListByType(@Param("type") String type,@Param("schoolCode") String schoolCode);

    @Select("select w.* from weixin_menu w where (school_code is null or school_code = '') and FIND_IN_SET(#{type},w.type)")
    List<WeixinMenuEntity> getWeixinMenuList(@Param("type") String type);

    @Select("select * from weixin_type")
    List<WeixinTypeEntity> getWeixinTypeList();

    @Select("select * from weixin_type where FIND_IN_SET(id,#{type})")
    List<WeixinTypeEntity> getWeixinTypByType(@Param("type") String type);

    @Select("select w.school_code from weixin_menu w where id = #{id}")
    String getSchoolCodeById(@Param("id") int id);

    @Insert("insert into weixin_menu (menu_name,path_url,type,order_num,school_code) select menu_name,path_url,type,order_num,${schoolCode} from weixin_menu where school_code is null or school_code = ' ' ")
    void insertWeixinMenuToSchool(@Param("schoolCode") String schoolCode);
}
